﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class MembershipCard_Ticket_pass :WeiPage
{
     TicketBO tbo = new TicketBO();
    PersonInfoBO fbo = new PersonInfoBO();
    TicketForPersonBO pbo = new TicketForPersonBO();
    PasswordForStoreBO sbo = new PasswordForStoreBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                        // Response.Write(b[13]);
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {

                }

                if (Request.QueryString["ID"] != null)
                {
                    TicketForPerson p = pbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                    if (p != null)
                    {
                        Ticket t = tbo.GetModel(p.TicketID);
                        if (t != null)
                        {
                            lblName.Text = t.Name;
                            lblNo.Text = p.ID.ToString();
                        }
                    }
                }
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (hidd.Value == "")
        {
            AlertMessage("请输入密码");
            return;
        }
        PasswordForStore s = sbo.GetModel(hidd.Value);
        if (s == null)
        {
            AlertMessage("密码错误");
            return;
        }
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            Session["FromUserName"] = user;
            Session["ToUserName"] = user2;
            PersonInfo per = fbo.GetModel(user);
            if (per != null)
            {

            }

            if (Request.QueryString["ID"] != null)
            {
                TicketForPerson p = pbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                if (p != null)
                {
                    p.UseTime = DateTime.Now;
                    p.Password = s.StoreID.ToString();
                    pbo.Update(p);
                    Response.Redirect("myquan.aspx");
                }
            }
        }
    }
}